package portal.cmd;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.Semaphore;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import portal.beans.CmdBean;
import portal.cmd.beans.JhostBean;
import portal.common.MyStringUtils;
@Component
@Scope("singleton")
public class JHostsServices {
	@Autowired
	JHCmd jHCmd;
	private static List<JhostBean> hosts=null;
	private static long lastLoad=0;
	private static final long  Interval=3*1000;//15秒
	private static Semaphore seHosts=new Semaphore(1);
	public void loadHosts(){
		if(seHosts.tryAcquire()==false)return;
		try{
			//15秒内不重复刷新请求
			if((System.currentTimeMillis()-lastLoad)< Interval)
				return;
			lastLoad=System.currentTimeMillis();
			analysisjHosts(jHCmd.jhosts());
			analysisHostStat(jHCmd.jhosts_list_stat());
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			seHosts.release();
		}		
	}
	public List<JhostBean> getHosts(String[] arr){
		List<JhostBean> list = new ArrayList<>();
		//if(hosts==null ||hosts.size()==0)
		loadHosts();
		if(hosts==null)return null;
		for(String s:arr){
			if(s==null ||s.length()==0)
				continue;
			for(JhostBean b:hosts){
				if(s.equals(b.getHostName())){
					list.add(b);
					continue;
				}
			}
		}
		return list;
	}
	public JhostBean getHost(String name){
		return findHostByName(name);
	}
	public void analysisjHosts(String jhosts){
		String[] arr= jhosts.split("\n");
		if(hosts==null)
			hosts=new ArrayList<>();
		hosts.clear();
		for(String s:arr){
			if(s==arr[0]){
				//System.out.println(s);
				continue;
			}
			String[] nodeA= MyStringUtils.strictSpace(s).split(" ");
			JhostBean jb =new JhostBean(nodeA[0],Arrays.copyOfRange(nodeA,1,nodeA.length));
			hosts.add(jb);
		}
	}
	public void analysisHostStat(String stat){
		String[] arr= stat.split("\n");
		if(hosts==null)
			hosts=new ArrayList<>();
		for(String s:arr){
			if(s==arr[0]){
				//System.out.println(s);
				continue;
			}
			String[] nodeA= MyStringUtils.strictSpace(s).split(" ");
			JhostBean jb =findHostByName(nodeA[0]);//n//ew JhostBean(nodeA[0],Arrays.copyOfRange(nodeA,1,nodeA.length-1));
			if(jb!=null){
				jb.setHostStat(Arrays.copyOfRange(nodeA,2,nodeA.length));
			}
			//hosts.add(jb);
		}
	}
	/*public int MAXfor(String name){
		return getValueByMethod(findHostByName(name),"MAX");
	}
	public int RUNfor(String name){
		return getValueByMethod(findHostByName(name),"RUN");
	}*/
	
	public int getHostPropertyInt(String hostName,String pro){
		return getValueByMethod(findHostByName(hostName),pro);
	}
	public String getHostPropertyString(String hostName,String pro){
		return null;//getValueByMethod(findHostByName(hostName),pro);
	}
	private JhostBean findHostByName(String name){
		if(name==null ||name .length()==0)
			return null;
		if(hosts==null ||hosts.size()==0 )
			loadHosts();
		for(JhostBean b:hosts){
			if(b.getHostName().equals(name))
				return b;
		}
		return null;
	}
	public int  getValueByMethod(JhostBean bean,String property){		
		Method method;
		try {
			method = Class.forName(bean.getClass().getName()).getMethod("get"+property);
			return (Integer) method.invoke(bean);
		} catch (NoSuchMethodException e) {
		} catch (SecurityException e) {
		} catch (ClassNotFoundException e) {
		} catch (IllegalAccessException e) {
		} catch (IllegalArgumentException e) {
		} catch (InvocationTargetException e) {
		}		
		return 0;
	}
	public List<CmdBean> checkNodes() {
		ArrayList<CmdBean> list=new ArrayList<>();
		String[] data = jHCmd.jhosts_list_stat().split("\n");
		for(String s :data){
			if(s==data[0]){
				if(s.startsWith("HOST_NAME"))
					continue;
			}
			CmdBean cb =new CmdBean();
			if(cb.setCpuAndMemU(s)==true)
				list.add(cb);
		}
		data =jHCmd.jhosts_attrib().split("\n");
		for(String s :data){
			if(s==data[0]){
				if(s.startsWith("HOST_NAME"))
					continue;
				break;
			}
			for(CmdBean b:list){
				if(b.setMemT(s))
					break;
			}
		}
		return list;
	}
}
